package com.nathan.crm.service;

import com.nathan.base.BaseService;
import com.nathan.crm.dao.CustomerLossMapper;
import com.nathan.crm.utils.AssertUtil;
import com.nathan.crm.vo.CustomerLoss;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CustomerLossService extends BaseService<CustomerLoss,Integer> {

    @Resource
    private CustomerLossMapper customerLossMapper;

    /**
     * 修改流失客户state为1，表示确定为不可挽回的客户
     * @param id
     * @param lossReason
     */
    @Transactional
    public void updateCustomerLossStateById(Integer id, String lossReason) {
        /* 参数校验 */
        AssertUtil.isTrue(id == null,"客户不存在！");
        AssertUtil.isTrue(StringUtils.isBlank(lossReason),"请输入流失原因！");

        // 根据id获取流失客户信息
        CustomerLoss customerLoss = customerLossMapper.selectByPrimaryKey(id);
        // 设置值 state=1（确认流失）
        customerLoss.setState(1);
        customerLoss.setLossReason(lossReason);
        customerLoss.setConfirmLossTime(new Date());

        /* 执行更新操作 */
        AssertUtil.isTrue(customerLossMapper.updateByPrimaryKeySelective(customerLoss) != 1,"更新失败！");
    }
}
